package com.robooot.business_counter.service;

import com.alibaba.fastjson.JSON;

import com.robooot.business_counter.config.IBizService;
import com.robooot.business_counter.dao.CourierDao;
import com.robooot.business_counter.dao.StoreDao;
import com.robooot.business_counter.dao.UserDao;
import com.robooot.business_counter.entity.*;
import com.robooot.business_counter.exception.BizException;
import com.robooot.business_counter.model.*;
import com.robooot.business_counter.util.DateUtil;
import com.robooot.business_counter.util.GUID;
import com.robooot.common.core.util.FlashResultUtil;
import net.sf.json.JSONObject;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.BeanInitializationException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.text.MessageFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class StoreService implements IBizService {

    @Autowired
    private StoreDao storeDao;

    @Autowired
    private CourierDao courierDao;

    @Autowired
    private RemoteUrl remoteUrl;
    @Autowired
    private UserDao userDao;

    private final Log log = LogFactory.getLog(getClass());

    public Integer getPickupRecordCount(StoreVo storeVo) {
        return storeDao.getPickupRecordCount(storeVo);
    }

    //获取存件记录和取件记录
    public List<Store> getPickupRecordList(StoreVo storeVo) {
        List<Store> list = storeDao.getPickupRecordList(storeVo);
        if(CollectionUtils.isEmpty(list)){
            return new ArrayList<>();
        }
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        list.forEach(a ->{
            a.setAddress(a.getMitName()+a.getShiName()+a.getQuName()+a.getLocation());
            String storageTime = a.getStorageTime();
            if (!StringUtils.isEmpty(storageTime)) {
                try {
                    Date parse = dateFormat.parse(storageTime);
                    Date date = new Date();
                    int between = DateUtil.daysBetween(parse, date);
                    a.setDay(between);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }
        });
        return list;
    }

    public List<Store> getUserStoreList(UserVo userVo) {
        List<Store> list = storeDao.getUserStoreList(userVo);
        if(CollectionUtils.isEmpty(list)){
            return new ArrayList<>();
        }
        list.forEach(a ->{
            a.setAddress(a.getMitName()+a.getShiName()+a.getQuName()+a.getLocation());
        });
        return list;
    }

    public List<Device> getDeviceDoor(DeviceVo deviceVo) {
        deviceVo.setStatus("Z");
        List<Device> arrayList = new ArrayList<>();
        List<DeviceDoor> justList = storeDao.getDeviceDoorJust(deviceVo);
        for (DeviceDoor deviceDoor : justList) {
            Device device = new Device();
            device.setCounterNumber(deviceDoor.getCounterNumber());
            device.setIsOut(deviceDoor.getIsOut());
            device.setDoorJustStatus(deviceDoor.getDoorStatus());
            arrayList.add(device);
        }
        //如果集合为空
        if(CollectionUtils.isEmpty(arrayList)){
            List<Device> list = storeDao.getDeviceDoor(deviceVo);
            return list;
        }
        deviceVo.setStatus("F");
        ArrayList<Device> arrayList2 = new ArrayList<>();
        List<DeviceDoor> backList = storeDao.getDeviceDoorJust(deviceVo);
        for (int i = 0; i < backList.size(); i++) {
            DeviceDoor deviceDoor = backList.get(i);
            Device device = arrayList.get(i);
            device.setCounterNumber(deviceDoor.getCounterNumber());
            device.setIsOut(deviceDoor.getIsOut());
            device.setDoorBackStatus(deviceDoor.getDoorStatus());
            arrayList2.add(device);
        }
        return arrayList2;

    }

    public Integer getDeviceDoorCount(DeviceVo deviceVo) {
        return storeDao.getDeviceDoorCount(deviceVo);
    }

    public List<JSONObject> getDeviceList() {
        return storeDao.getDeviceList();
    }

    private final Logger logger = LoggerFactory.getLogger(getClass());

    //快递柜路由数据处理/开门
    @Transactional
    public Map handle(DevicePackage pg) {
        HashMap<String, Object> result = new HashMap<>();
        //查看获取参数
        logger.info("device upload : " + JSON.toJSONString(pg));
        String deviceId = pg.getDeviceId();
        Map params = pg.getData();
        String sequence = String.valueOf(params.get("sequence"));
        String action = (String) params.get("action");
        //判断是否有该设备
        JSONObject device = storeDao.getDevice(deviceId);
        if(device == null){
            throw new BizException("没有查询到该设备信息");
        }
        if (!"Z".equals(device.getString("device_status"))) {
            throw new BizException("该设备已出故障正在维修,请稍后操作");
        }
        result.put("sequence",params.get("sequence"));
        result.put("action",action);
        Map user = (Map) params.get("user");

        switch(action){
            //查询
            case ACTION_QUERY:
                //查询用户打开快递柜故障,重新打开
               JSONObject storeInfoTow = storeDao.getStoreInfoTow(sequence);
               if(storeInfoTow != null){
                   String device_id = storeInfoTow.getString("device_id");
                   String counterNumber = storeInfoTow.getString("counter_number");
                   String storeDeviceState = storeInfoTow.getString("device_state");
                   //查询门的信息
                   JSONObject jsonObject = storeDao.getDoorStatus(device_id,counterNumber,storeDeviceState);
                   //查询设备信息
                   JSONObject queryDevice = storeDao.getDevice(deviceId);
                   String doorStatus = jsonObject.getString("door_status");
                   //判断门必须是关的才可以开门
                   if("G".equals(doorStatus)){
                       //判断是否可以让用户从正面开门或者是反面
                       String deviceState = queryDevice.getString("device_state");
                       String doorDeviceState = jsonObject.getString("device_state");
                       if("W".equals(deviceState)){
                           result.put("callback",1);
                           result.put("tradeNo", GUID.getRandom(26));
                           if("Z".equals(doorDeviceState)) result.put("lockIndex",0);
                           if("F".equals(doorDeviceState)) result.put("lockIndex",1);
                           result.put("boxNumber", jsonObject.getInt("box_number"));
                           result.put("errorType",0);
                           return result;
                       } else if(deviceState.equals(doorDeviceState)){
                           result.put("callback",4);
                           result.put("errorType",4);
                           if (doorDeviceState.equals("Z")) throw new BizException("请从反面开门");
                           if (doorDeviceState.equals("F")) throw new BizException("请从正面开门");
                           return result;
                       }else {
                           result.put("callback",1);
                           result.put("tradeNo", GUID.getRandom(26));
                           if("Z".equals(doorDeviceState)) result.put("lockIndex",0);
                           if("F".equals(doorDeviceState)) result.put("lockIndex",1);
                           result.put("boxNumber", jsonObject.getInt("box_number"));
                           result.put("errorType",0);
                           return result;
                       }
                   }else {
                       result.put("callback",4);
                       result.put("errorType",4);
                       return result;
                   }
               }else {
                   result.put("callback",4);
                   result.put("errorType",4);
                   return result;
               }
            //使用手机闪光方式开门
            case ACTION_FLASH:
                //判断是否是快递员使用
                String phone = (String) user.get("name");
                String nickName = (String) user.get("nickName");
                JSONObject courier = courierDao.getCourierInfo(phone);
                //如果快递员信息为空的话以下均为用户操作
                if(courier == null){
                    JSONObject userInfo = storeDao.getUserInfo((String) user.get("id"));
                    //用户不存在保存用户信息
                    if(userInfo == null){
                        //当该用户不存在与数据库中才需保存
                        logger.info("保存===================");
                        //保存用户信息
                        UserInfo info = new UserInfo((String) user.get("id"),"",phone,nickName);
                        userDao.saveUserInfo(info);
                    }
                        int boxNumber = (int) params.get("boxNumber");
                        JSONObject deviceDoor = storeDao.getDeviceDoorStatus(deviceId,boxNumber);
                        //查看该用户的存储记录,有没有存放物品
                        JSONObject storeInfo = storeDao.getStoreInfo((String) user.get("name"),deviceDoor.getInt("counterNumber"),deviceId);
                        //用户有代取件的物品才可以开门
                        if (storeInfo != null){
                            if(boxNumber % 2 == 0){
                                //改变相应的订单数据
                                storeDao.saveStoreFlash(sequence,storeInfo.getString("id"),"F");
                                JSONObject counterNumber = courierDao.getCounterNumber(deviceId, String.valueOf(boxNumber));
                                storeDao.updateDeviceDoor(deviceId,Integer.valueOf(counterNumber.getString("counterNumber")));
                            }else if(boxNumber % 2 == 1){
                                //改变相应的订单数据
                                storeDao.saveStoreFlash(sequence,storeInfo.getString("id"),"Z");
                                JSONObject counterNumber = courierDao.getCounterNumber(deviceId, String.valueOf(boxNumber));
                                storeDao.updateDeviceDoor(deviceId,Integer.valueOf(counterNumber.getString("counterNumber")));
                            }
                            if("ON".equals(device.getString("interlock"))){
                                //互锁(两边门都是关着的情况才可以开门)
                                interlock(deviceId,boxNumber);
                            }
                            String deviceState = device.getString("device_state");
                            String doorDeviceState = storeDao.findDoorDeviceState(deviceId,(int) params.get("boxNumber"));
                            if("W".equals(deviceState)){
                                //开门
                                result.put("callback",1);
                                result.put("tradeNo",GUID.getRandom(26));
                                result.put("lockIndex",0);
                                result.put("boxNumber", params.get("boxNumber"));
                                result.put("errorType",0);
                                JSONObject counterNumber = courierDao.getCounterNumber(deviceId, String.valueOf(boxNumber));
                                String url = MessageFormat.format(remoteUrl.getNotify(),counterNumber.getString("counterNumber"));
                                result.putAll(FlashResultUtil.url(url, false));
                                return result;
                            }else if (deviceState.equals(doorDeviceState)){
                                if (doorDeviceState.equals("Z")) throw new BizException("请从反面开门");
                                if (doorDeviceState.equals("F")) throw new BizException("请从正面开门");
                                result.put("callback",4);
                                result.put("errorType",4);
                                return result;
                            }else {
                                result.put("callback",1);
                                if("Z".equals(doorDeviceState)) result.put("lockIndex",0);
                                if("F".equals(doorDeviceState)) result.put("lockIndex",1);
                                result.put("tradeNo",GUID.getRandom(26));
                                result.put("boxNumber", params.get("boxNumber"));
                                result.put("errorType",0);
                                JSONObject counterNumber = courierDao.getCounterNumber(deviceId, String.valueOf(boxNumber));
                                String url = MessageFormat.format(remoteUrl.getNotify(),counterNumber.getString("counterNumber"));
                                result.putAll(FlashResultUtil.url(url, false));
                                return result;
                            }
                        //用户没有快递
                        }else {
                            result.put("callback",4);
                            result.put("errorType",3);
                            throw new BizException("你没有可以拿走的快递");
                        }
                 //快递员操作
                }else {
                    if("ON".equals(device.getString("interlock"))){
                        //互锁(两边门都是关着的情况才可以开门)
                        interlock(deviceId,(int) params.get("boxNumber"));
                    }
                    //判断快递员是否是第一次打开柜门(是 : 存件开门, 不是取件开门)
                    String courierPhone = courier.getString("phone");
                    JSONObject deviceDoor = storeDao.getDeviceDoorStatus(deviceId,(int) params.get("boxNumber"));
                    //查看存储记录,有没有存放物品
                    JSONObject storeInfo = storeDao.getStoreInfoTwo(courierPhone,deviceDoor.getInt("counterNumber"),deviceId);
                    String deviceState = device.getString("device_state");
                    String doorDeviceState = storeDao.findDoorDeviceState(deviceId,(int) params.get("boxNumber"));
                    if(storeInfo != null){
                        String courier_phone = storeInfo.getString("courier_phone");
                        if(courier.getString("phone").equals(courier_phone)){
                            //快递员取件开门操作
                            storeDao.updateStoreInfo(storeInfo.getInt("id"),courier.getString("name"),courier.getString("phone"),"C");
                        }else {
                            storeDao.updateStoreInfo(storeInfo.getInt("id"),courier.getString("name"),courier.getString("phone"),"Y");
                        }
                        return openLockTwo(result,deviceState,doorDeviceState,(int) params.get("boxNumber"),deviceId);
                    }

                    //快递员存件开门操作
                    return openLock(result,deviceState,doorDeviceState,(int) params.get("boxNumber"),deviceId);
                }

            //使用光ID方式开门
            case ACTION_FLASH_KEY:
                //判断是否是快递员使用
                String phone2 = (String) user.get("name");
                String nickName2 = (String) user.get("nickName");
                JSONObject courier2 = courierDao.getCourierInfo(phone2);
                log.info("光ID用户手机号==========" + phone2);
                //如果快递员信息为空的话以下均为用户操作
                if(courier2 == null) {
                    //查看该用户的存储记录,有没有存放物品
                    JSONObject userInfoTow = storeDao.getUserInfo((String) user.get("id"));
                    //用户不存在保存用户信息
                    if(userInfoTow == null){
                        //当该用户不存在与数据库中才需保存
                        logger.info("保存===================");
                        //保存用户信息
                        UserInfo info = new UserInfo((String) user.get("id"),"",phone2,nickName2);
                        userDao.saveUserInfo(info);
                    }
                        int boxNumber = (int) params.get("boxNumber");
                        JSONObject deviceDoor = storeDao.getDeviceDoorStatus(deviceId,boxNumber);
                        //查看该用户的存储记录,有没有存放物品
                        JSONObject storeInfo = storeDao.getStoreInfo(phone2,deviceDoor.getInt("counterNumber"),deviceId);
                        if (storeInfo != null) {
                            //改变相应的订单数据
                            Long flashKeySn = (Long) params.get("flashKeySn");
                            if (boxNumber % 2 == 1) {
                                storeDao.saveStore(sequence, String.valueOf(flashKeySn), storeInfo.getString("id"), "Z");
                                JSONObject counterNumber = courierDao.getCounterNumber(deviceId, String.valueOf(boxNumber));
                                storeDao.updateDeviceDoor(deviceId, Integer.valueOf(counterNumber.getString("counterNumber")));
                            } else if (boxNumber % 2 == 0) {
                                storeDao.saveStore(sequence, String.valueOf(flashKeySn), storeInfo.getString("id"), "F");
                                JSONObject counterNumber = courierDao.getCounterNumber(deviceId, String.valueOf(boxNumber));
                                storeDao.updateDeviceDoor(deviceId, Integer.valueOf(counterNumber.getString("counterNumber")));
                            }
                            if("ON".equals(device.getString("interlock"))){
                                //互锁(两边门都是关着的情况才可以开门)
                                interlock(deviceId,(int) params.get("boxNumber"));
                            }
                            String deviceState = device.getString("device_state");
                            String doorDeviceState = storeDao.findDoorDeviceState(deviceId,(int) params.get("boxNumber"));
                            if("W".equals(deviceState)){
                                //开门
                                result.put("callback",1);
                                result.put("lockIndex",0);
                                result.put("tradeNo",GUID.getRandom(26));
                                result.put("boxNumber",boxNumber);
                                result.put("errorType",0);
                                JSONObject counterNumber = courierDao.getCounterNumber(deviceId, String.valueOf(boxNumber));
                                String url = MessageFormat.format(remoteUrl.getNotify(),counterNumber.getString("counterNumber"));
                                result.putAll(FlashResultUtil.url(url, false));
                                return result;
                            }else if (deviceState.equals(doorDeviceState)){
                                if (doorDeviceState.equals("Z")) throw new BizException("请从反面开门");
                                if (doorDeviceState.equals("F")) throw new BizException("请从正面开门");
                                result.put("callback",4);
                                result.put("errorType",4);
                                return result;
                            }else {
                                //开门
                                result.put("callback",1);
                                if("Z".equals(doorDeviceState)) result.put("lockIndex",0);
                                if("F".equals(doorDeviceState)) result.put("lockIndex",1);
                                JSONObject counterNumber = courierDao.getCounterNumber(deviceId, String.valueOf(boxNumber));
                                String url = MessageFormat.format(remoteUrl.getNotify(),counterNumber.getString("counterNumber"));
                                result.putAll(FlashResultUtil.url(url, false));
                                result.put("tradeNo",GUID.getRandom(26));
                                result.put("boxNumber", boxNumber);
                                result.put("errorType",0);
                                return result;
                            }
                        //用户没有可取的快递
                        }else {
                            result.put("callback",4);
                            result.put("errorType",3);
                            throw new BizException("没有可以拿走的快递");
                        }
                 //快递员操作
                }else {
                    if("ON".equals(device.getString("interlock"))){
                        //互锁(两边门都是关着的情况才可以开门)
                        interlock(deviceId,(int) params.get("boxNumber"));
                    }
                    //判断快递员是否是第一次打开柜门(是 : 存件开门, 不是取件开门)
                    String courierPhone = courier2.getString("phone");
                    JSONObject deviceDoor = storeDao.getDeviceDoorStatus(deviceId,(int) params.get("boxNumber"));
                    //查看存储记录,有没有存放物品
                    JSONObject storeInfo = storeDao.getStoreInfoTwo(courierPhone,deviceDoor.getInt("counterNumber"),deviceId);
                    String deviceState = device.getString("device_state");
                    String doorDeviceState = storeDao.findDoorDeviceState(deviceId,(int) params.get("boxNumber"));
                    if(storeInfo != null){
                        //快递员取件开门操作
                        if(courier2.getString("phone").equals("courier_phone")){
                            //快递员取件开门操作
                            storeDao.updateStoreInfo(storeInfo.getInt("id"),courier2.getString("name"),courier2.getString("phone"),"C");
                        }else {
                            storeDao.updateStoreInfo(storeInfo.getInt("id"),courier2.getString("name"),courier2.getString("phone"),"Y");
                        }
                        return openLockTwo(result,deviceState,doorDeviceState,(int) params.get("boxNumber"),deviceId);
                    }
                    //快递员存件开门操作
                    return openLock(result,deviceState,doorDeviceState,(int) params.get("boxNumber"),deviceId);
                }
            //状态同步
            case ACTION_STATUS:
                Object boxesInfo = params.get("boxesInfo");
                log.info("转换前的数据========="+JSON.toJSONString(boxesInfo));
                if (boxesInfo != null){
                    String jsonString = JSON.toJSONString(boxesInfo);
                    List<BoxInfo> boxInfos = JSON.parseArray(jsonString,BoxInfo.class);
                    for (BoxInfo boxInfo : boxInfos) {
                        Integer boxInfoNumber = boxInfo.getNumber();
                        Integer status = boxInfo.getDoorsInfo().get(0).getStatus();
                        if(0 == status){
                            log.info("改变门状态===="+deviceId+"======"+boxInfoNumber);
                            storeDao.updateBoxDoor(boxInfoNumber,"K",deviceId);
                        }else if(1 == status){
                            storeDao.updateBoxDoor(boxInfoNumber,"G",deviceId);
                        }
                    }
                    log.info("转换后的数据========="+JSON.toJSONString(boxInfos));
                }

                log.info("max 数量====" + params.get("boxSize"));
                int max = (int) params.get("boxSize");
                List<JSONObject> deviceDoor = storeDao.findDeviceDoorStatus(deviceId);
                List<JSONObject> list = new ArrayList<>();
                for (int i = 0; i < max; i++) {
                    JSONObject map = new JSONObject();
                    map.put("boxNumber",deviceDoor.get(i).getInt("boxNumber"));
                    map.put("status",deviceDoor.get(i).getInt("status"));
                    list.add(map);
                }
                result.put("interval",10);
                result.put("boxList",list);
                return result;

        }
        throw new BizException("操作失败请查看设备是否正常");
    }

    /**
     * 快递员存件操作
     * @param result
     * @param deviceState
     * @param doorDeviceState
     * @param boxNumber
     * @return
     */
    private HashMap<String,Object> openLock(HashMap<String, Object> result,String deviceState,String doorDeviceState,Integer boxNumber,String deviceId){
        if("W".equals(deviceState)){
            //开门
            result.put("callback",2);
            result.put("lockIndex",0);
            result.put("tradeNo",GUID.getRandom(26));
            result.put("boxNumber", boxNumber);
            result.put("errorType",0);
            JSONObject counterNumber = courierDao.getCounterNumber(deviceId, String.valueOf(boxNumber));
            String url = MessageFormat.format(remoteUrl.getKuaidiyuannotify(),counterNumber.getString("counterNumber"),deviceId);
            result.putAll(FlashResultUtil.url(url, true));
            return result;
        }else if (deviceState.equals(doorDeviceState)){
            //开门
            result.put("callback",2);
            if("Z".equals(doorDeviceState)) result.put("lockIndex",0);
            if("F".equals(doorDeviceState)) result.put("lockIndex",1);
            result.put("tradeNo",GUID.getRandom(26));
            result.put("boxNumber", boxNumber);
            result.put("errorType",0);
            JSONObject counterNumber = courierDao.getCounterNumber(deviceId, String.valueOf(boxNumber));
            String url = MessageFormat.format(remoteUrl.getKuaidiyuannotify(),counterNumber.getString("counterNumber"),deviceId);
            result.putAll(FlashResultUtil.url(url, true));
            return result;
        }else {
            if (doorDeviceState.equals("Z")) throw new BizException("请从反面开门");
            if (doorDeviceState.equals("F")) throw new BizException("请从正面开门");
            result.put("callback",4);
            result.put("errorType",4);
            return result;
        }
    }

    /**
     * 快递员取件操作
     * @param result
     * @param deviceState
     * @param doorDeviceState
     * @param boxNumber
     * @return
     */
    private HashMap<String,Object> openLockTwo(HashMap<String, Object> result,String deviceState,String doorDeviceState,Integer boxNumber,String deviceId){
        if("W".equals(deviceState)){
            //开门
            result.put("callback",1);
            result.put("lockIndex",0);
            result.put("tradeNo",GUID.getRandom(26));
            result.put("boxNumber", boxNumber);
            result.put("errorType",0);
            JSONObject counterNumber = courierDao.getCounterNumber(deviceId, String.valueOf(boxNumber));
            storeDao.updateDeviceDoor(deviceId,Integer.valueOf(counterNumber.getString("counterNumber")));
            String url = MessageFormat.format(remoteUrl.getNotify(),counterNumber.getString("counterNumber"));
            result.putAll(FlashResultUtil.url(url, false));
            return result;
        }else if (deviceState.equals(doorDeviceState)){
            //开门
            result.put("callback",1);
            if("Z".equals(doorDeviceState)) result.put("lockIndex",0);
            if("F".equals(doorDeviceState)) result.put("lockIndex",1);
            result.put("tradeNo",GUID.getRandom(26));
            result.put("boxNumber", boxNumber);
            result.put("errorType",0);
            JSONObject counterNumber = courierDao.getCounterNumber(deviceId, String.valueOf(boxNumber));
            storeDao.updateDeviceDoor(deviceId,Integer.valueOf(counterNumber.getString("counterNumber")));
            String url = MessageFormat.format(remoteUrl.getNotify(),counterNumber.getString("counterNumber"));
            result.putAll(FlashResultUtil.url(url, false));
            return result;
        }else {
            if (doorDeviceState.equals("Z")) throw new BizException("请从反面开门");
            if (doorDeviceState.equals("F")) throw new BizException("请从正面开门");
            result.put("callback",4);
            result.put("errorType",4);
            return result;
        }
    }


    /**
     * 快递柜互锁 : 用于快递柜是否正反面都关门的,
     */
    private void interlock(String deviceId,Integer boxNumber){
        //查找出他是属于哪一个柜子
        JSONObject counterNumber = storeDao.getDeviceDoorStatus(deviceId, boxNumber);
        if (counterNumber == null){
            throw new BizException("没有对应的柜子,请初始化好再操作");
        }
        List<JSONObject> list = storeDao.findDeviceDoor(counterNumber.getString("counterNumber"),deviceId);
        for (JSONObject jsonObject : list) {
            if (boxNumber != jsonObject.getInt("boxNumber")) {
                String doorStatus = jsonObject.getString("doorStatus");
                if ("K".equals(doorStatus)) {
                    throw new BizException("请关闭好柜子的另一端再进行操作");
                }
            }
        }

    }

    /**
     * 快递员,存放快递
     * @param checkVo
     */
    public void bagCheck(CheckVo checkVo) {
        //改变柜门状态
        courierDao.updateDevice(checkVo.getDeviceId(),checkVo.getCounterNumber());
        //存放物件
        storeDao.bagCheck(checkVo);
    }

    public JSONObject getDevice(String deviceId) {
        return storeDao.getDevice(deviceId);
    }

    public JSONObject getDeviceLocation(String deviceId) {
        return storeDao.getDeviceLocation(deviceId);
    }


    /**
     * 快递柜关门操作
     * 每两分钟执行一次
     * 同步一下快递员存件关门
     */
//    @Scheduled(cron = "0 */1 * * * ?")
//    public void close(){
//        List<JSONObject> deviceDoor = storeDao.getDeviceDoorList();
//        for (JSONObject jsonObject : deviceDoor) {
//            courierDao.closeDeviceDoor(jsonObject.getString("counterNumber"),jsonObject.getString("deviceId"));
//        }
//    }


}
